<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>房产中介管理系统</title>
<style type="text/css">
html, body {
	font:normal 12px verdana;
	margin:0;
	padding:0;
	border:0 none;
	overflow:hidden;
	height:100%;
}
#search { padding-top:10px;padding-left:10px;}
form#DialogForm, table#DetailTable {display:none;}
.noblank {color: #A52A2A}
.tip {float: left}
</style>
</head>
<body>
<div id="loading">
  <div class="loading-indicator"><img src="<?=base_url()?>js/ext/resources/images/default/grid/loading.gif" style="width:16px;height:16px;" align="absmiddle">&#160;装载中...</div>
</div>
<!-- include everything after the loading indicator -->
<link rel="stylesheet" type="text/css" href="<?=base_url()?>js/ext/resources/css/ext-all.css" />
<!--<link rel="stylesheet" type="text/css" href="js/ext/resources/css/ytheme-aero.css" />
<link rel="stylesheet" type="text/css" href="js/ext/resources/css/ytheme-vista.css" />-->
<link rel="stylesheet" type="text/css" href="<?=base_url()?>js/ext/resources/css/ytheme-gray.css" />
<link rel="stylesheet" type="text/css" href="<?=base_url()?>css/loading.css" />
<script type="text/javascript" src="<?=base_url()?>js/ext/adapter/yui/yui-utilities.js"></script>
<script type="text/javascript" src="<?=base_url()?>js/ext/adapter/yui/ext-yui-adapter.js"></script>
<script type="text/javascript" src="<?=base_url()?>js/ext/ext-all-debug.js"></script>
<script type="text/javascript" src="<?=base_url()?>js/ext/ext-lang-zh.js"></script>
<script type="text/javascript">
var LayoutObject;
var TabObject;
var DataStore;
var SaveButton;
var	BASE_URL = "<?=site_url()?>";
// 当整个文件全部装载完成后执行
Ext.onReady(function(){
    Ext.QuickTips.init();

	LayoutObject = new Ext.BorderLayout(document.body, {
        north: {
            split:false,
            initialSize: 70,
            titlebar: false
	        },
        south: {
            split:false,
            initialSize: 203,
			fitToFrame:true,
            titlebar: false
        },
        center: {
            autoScroll:true,
//            tabPosition: 'top',
//            closeOnTab: true,
//            alwaysShowTabs: true,
//            resizeTabs: true,
			fitToFrame:true
        }
    });

	LayoutObject.beginUpdate();
	LayoutObject.add('north', new Ext.ContentPanel('top'));
	LayoutObject.add('south', new Ext.ContentPanel('bottom', {fitToFrame:true}));
	LayoutObject.add('center', new Ext.ContentPanel('content', {fitToFrame:true}));
    //layout.restoreState();
	LayoutObject.endUpdate();

    TabObject = new Ext.TabPanel("bottom");
    //TabObject.addTab("zidian", "字典");
    //TabObject.addTab("genjin", "跟进");
    //TabObject.addTab("pipei", "匹配");
    TabObject.addTab("details", "详情");
    //TabObject.addTab("rizhi", "日志");
    //TabObject.addTab("jilu", "电话查看记录");
    TabObject.activate("details");

    var tb = new Ext.Toolbar('toolbar');
    tb.add({
//            cls: 'x-btn-text-icon bmenu', // icon and text class
            id:'ToolbarAll',
            text:'全部',
	        enableToggle: true,
			toggleGroup: 'type',
	        toggleHandler: onToolbarToggle
//	        pressed: true
    });
    tb.add({
            id:'ToolbarHire',
            text:'求租',
	        enableToggle: true,
			toggleGroup: 'type',
	        toggleHandler: onToolbarToggle
    });
    tb.add({
            id:'ToolbarSale',
            text:'求购',
	        enableToggle: true,
			toggleGroup: 'type',
	        toggleHandler: onToolbarToggle
    });
    tb.add('separator');
    tb.add({
            id:'AddItem',
            text:'增加',
            handler:function(e){AddDialog.show();}
    });
    tb.add({
            id:'EditItem',
            text:'编辑',
            handler:function(e){
		    	if (grid.getSelectionModel().getSelected())
		    	{
	           		EditDialog.show();
	           	}
	           	else
	           	{
					Ext.MessageBox.show({
					   title:'房源管理',
					   msg: '请先选择一条房源信息再编辑！',
					   buttons: Ext.MessageBox.OK,
					   animEl: 'EditItem'
					});
	           	}
            }
    });
    tb.add({
    		id:'DeleteItem',
    		text:'删除',
    		handler:function(e){DeleteItems();}
    });


	// 新增对话框
	var AddDialog = new Ext.BasicDialog("AddDialog", {
        height: 500,
        width: 570,
        closable:false,
        resizable:false,
        title:'客源管理 - 新增',
        modal: true,
        animateTarget: 'AddItem',
        proxyDrag: true,
        autoScroll:false,
        //shim:true,
        shadow:false
    });
    AddDialog.addButton({disabled:true,text:'保存'}, function(e){
    	//alert(YAHOO.util.Connect.setForm('DialogForm'));
    	//return;
    	// 检查添加表单里的各种条件(基本条件已经在对话框里限制了)
    	AddDialog.getEl().mask('保存中...');
		// 提交添加表单！
		Ext.get('SaveLayer').load(
			{
				url: BASE_URL + "/customer/manager/addsave",
				params: YAHOO.util.Connect.setForm('DialogForm'), // or a URL encoded string
				callback: function(oElement, bSuccess, oResponse){
					//alert(oResponse.responseText);
					var doc = oResponse.responseXML;
					doc = doc.documentElement || doc;
					var el = Ext.DomQuery.selectNode("//SaveResponse", doc);
					if (el)
					{
						var response = el.textContent || el.text;
						if (response=='true')
						{
							SubmitForm();
							Ext.MessageBox.alert('新增客源', '保存成功！', function(){
									AddDialog.getEl().unmask();
									AddDialog.hide();
							    }
							);
						}
						else
						{
							Ext.MessageBox.alert('新增客源', '保存失败！请重试！', function(){
									AddDialog.getEl().unmask();
							    }
							);
						}
					}
					else
					{
						Ext.MessageBox.alert('新增客源', '保存失败！请重试！', function(){
								AddDialog.getEl().unmask();
						    }
						);
					}
				},
				nocache: true,
				scripts: false
			 }
	    );
   	}, AddDialog);
    AddDialog.addButton('取消', AddDialog.hide, AddDialog);
    AddDialog.footer.insertHtml('beforeBegin','<div class="tip">注意: <span class="noblank">红字</span>为必填项目，<br />“保存”按钮将在所有必填项目正确填写后方可使用。</div>');
    
    // 定义 添加 对话框事件
    AddDialog.on('show',function(e){
	    	SaveButton = AddDialog.buttons[0];
	    	SaveButton.disable();
	    	e.body.load({
					 url: BASE_URL + "/customer/manager/add",
					 nocache: true,
					 scripts: true
				},'dialog'
			)
		}
	);
	AddDialog.on('hide',function(e){AddDialog.body.child("#body").remove();});


	// 编辑对话框
	var EditDialog = new Ext.BasicDialog('EditDialog', {
        height: 500,
        width: 570,
        closable:false,
        resizable:false,
        title:'客源管理 - 编辑',
        modal: true,
        animateTarget: 'EditItem',
        proxyDrag: true,
        autoScroll:false,
        shadow:false
    });
    EditDialog.addButton({disabled:true,text:'保存'}, function(e){
    	// 检查添加表单里的各种条件(基本条件已经在对话框里限制了)
    	EditDialog.getEl().mask('保存中...');
		// 提交添加表单！
		Ext.get('SaveLayer').load(
			{
				url: BASE_URL + '/customer/manager/editsave',
				params: YAHOO.util.Connect.setForm('DialogForm'), // or a URL encoded string
				callback: function(oElement, bSuccess, oResponse){
					//alert(oResponse.responseText);
					var doc = oResponse.responseXML;
					doc = doc.documentElement || doc;
					var el = Ext.DomQuery.selectNode('//SaveResponse', doc);
					if (el)
					{
						var response = el.textContent || el.text;
						if (response=='true')
						{
							SubmitForm();
							Ext.MessageBox.alert('编辑客源', '保存成功！', function(){
									EditDialog.getEl().unmask();
									EditDialog.hide();
							    }
							);
						}
						else
						{
							Ext.MessageBox.alert('编辑客源', '保存失败！请重试！', function(){
									EditDialog.getEl().unmask();
							    }
							);
						}
					}
					else
					{
						Ext.MessageBox.alert('编辑客源', '保存失败！请重试！', function(){
								EditDialog.getEl().unmask();
						    }
						);
					}
				},
				nocache: true,
				scripts: false
			 }
	    );
   	}, EditDialog);
    EditDialog.addButton('取消', EditDialog.hide, EditDialog);
    EditDialog.footer.insertHtml('beforeBegin','<div class="tip">注意: <span class="noblank">红字</span>为必填项目，<br />“保存”按钮将在所有必填项目正确填写后方可使用。</div>');
    
    // 定义 编辑 对话框事件
    EditDialog.on('show',function(e){
	    	SaveButton = EditDialog.buttons[0];
	    	SaveButton.disable();
	    	var CurrentID = grid.getSelectionModel().getSelected().get('id');
	    	if (CurrentID=='') CurrentID=1;
	    	e.body.load({
					 url: BASE_URL + "/customer/manager/edit/" + CurrentID,
					 nocache: true,
					 scripts: true
				},'dialog'
	    	)
	    }
    );
    EditDialog.on('hide',function(e){EditDialog.body.child("#body").remove();});


	var DeleteItems = function()
	{
		var IdList='';

		Ext.each(grid.getSelectionModel().getSelections(),function(obj){IdList += obj.get('id') + ',';});

		if (IdList=='')
		{
			Ext.MessageBox.show({
			   title:'客源管理',
			   msg: '请先选择一条或多条客源信息再删除！',
			   buttons: Ext.MessageBox.OK,
			   animEl: 'DeleteItem'
			});

			return;
		}
		Ext.MessageBox.confirm('删除客源', '您确定要删除这些客源信息吗？', function(btn){
			    if (btn == 'yes'){

			    	Ext.get(document.body).mask('删除中...');
					// 提交添加表单！
					Ext.get('SaveLayer').load(
						{
							url: BASE_URL + "/customer/manager/delete",
							params: {data: IdList},
							callback: function(oElement, bSuccess, oResponse){
								//alert(oResponse.responseText);
								var doc = oResponse.responseXML;
								doc = doc.documentElement || doc;
								var el = Ext.DomQuery.selectNode("//SaveResponse", doc);
								if (el)
								{
									var response = el.textContent || el.text;
									if (response=='true')
									{
										SubmitForm();
										Ext.MessageBox.alert('删除客源', '删除成功！');
									}
									else
									{
										Ext.MessageBox.alert('删除客源', '删除失败！请重试！');
									}
								}
								else
								{
									Ext.MessageBox.alert('删除客源', '删除失败！请重试！');
								}
								Ext.get(document.body).unmask();
							},
							nocache: true,
							scripts: false
						 }
				    );
			    }
			}
		);
	}


    // 搜索栏代码开始
    var purpose = new Ext.form.ComboBox({
        transform:'purpose',
        width:80,
        editable:false
    });
    new Ext.form.NumberField({
        width:30
    }).applyTo('house_typedown');
    new Ext.form.NumberField({
        width:30
    }).applyTo('house_typeup');
    new Ext.form.NumberField({
        width:40
    }).applyTo('price_down');
    new Ext.form.NumberField({
        width:40
    }).applyTo('price_up');
    new Ext.form.TextField({
        width:60
    }).applyTo('req_name');

	//new Ext.Button("grow",{text:'扩展'});
	new Ext.Button("clear",{text:'清除',handler:function(){Ext.get('search_form').dom.reset();purpose.reset();}});
	new Ext.Button("find",{text:'查询',handler:SubmitForm});
	Ext.get('search').setVisible(true,false);
	// 搜索栏代码结束


    // 创建主信息列表
    DataStore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({url: BASE_URL + '/customer/manager/search/'+Math.random(), method:'post'}),
        reader: new Ext.data.XmlReader({
               record: 'Item',
               id: 'id',
               totalRecords: 'total'
           }, [
               {name: 'id', mapping: 'id'},
               'if_rent', 'req_name', 'link_name', 'sex', 'req_grade', 'status','purpose',
               'house_type', 'price', 'area', 'source', 'wt_date', 'duty_userid'
           ]),
        remoteSort: false
    });
	DataStore.on('load',function(){
			var sm = grid.getSelectionModel();
			sm.selectFirstRow();
			if (!sm.hasSelection())
			{
				LoadContent(null, null);
			}
		}
	);
	DataStore.on('beforeload', function(){DataStore.baseParams = YAHOO.util.Connect.setForm('search_form');});

    var cm = new Ext.grid.ColumnModel([
	    {header: "编号", width: 50, dataIndex: 'id'},
		{header: "租购", width: 40, dataIndex: 'if_rent'},
		{header: "客户", width: 60, dataIndex: 'req_name'},
		{header: "联系人", width: 60, dataIndex: 'link_name'},
		{header: "性别", width: 35, dataIndex: 'sex'},
		{header: "等级", width: 35, dataIndex: 'req_grade'},
	    {header: "状态", width: 40, dataIndex: 'status'},
		{header: "用途", width: 60, dataIndex: 'purpose'},
		{header: "户型", width: 80, dataIndex: 'house_type'},
		{header: "价格范围", width: 80, dataIndex: 'price'},
	    {header: "面积范围", width: 80, dataIndex: 'area'},
		{header: "来源", width: 40, dataIndex: 'source'},
		{header: "委托日期", width: 70, dataIndex: 'wt_date', renderer:function(v){return v.split(' ')[0];}},
		//{header: "门店名称", width: 50, dataIndex: 'input_deptname'},
		{header: "负责人", width: 60, dataIndex: 'duty_userid'}
	]);
    cm.defaultSortable = true;

    // create the grid
    var grid = new Ext.grid.Grid('content', {
        ds: DataStore,
        cm: cm,
        trackMouseOver: true,
        loadMask: {msg:'装载中...'},
		selModel: new Ext.grid.RowSelectionModel({multipleSelect:true})
    });
    grid.getSelectionModel().on('rowselect',LoadContent);
    grid.on('celldblclick',function(){EditDialog.show()});
    grid.on('keydown',function(e){if (e.getKey()==e.DELETE){DeleteItems();}});
    grid.render();

	var gridFoot = grid.getView().getFooterPanel(true);

    // add a paging toolbar to the grid's footer
    var paging = new Ext.PagingToolbar(gridFoot, DataStore, {
        pageSize: 10,
        displayInfo: true,
        displayMsg: '总共: {2}, 当前: {0} - {1}',
        emptyMsg: "暂无信息"
    });

    SubmitForm();


	var loading = Ext.get('loading');
	loading.fadeOut({duration:.4,remove:true});

});
var SubmitForm = function(e)
{
	//Ext.MessageBox.alert('测试',YAHOO.util.Connect.setForm('search_form'));
	DataStore.load({params:{start: 0, limit: 10}});
};
var LoadContent = function(sm, rowIndex)
{
	//Ext.MessageBox.alert('测试',sm.getSelected().data['id']);
	var tab = TabObject.getTab('details');
	if (sm)
	{
		tab.setUrl({url: BASE_URL + "/customer/manager/detail/"+sm.getSelected().data['id'],nocache: true,scripts: false},'detail');
		tab.refresh();
	}
	else
	{
		tab.setContent('',false);
	}
};
var onToolbarToggle = function(item, pressed)
{
	switch(item.id)
	{
		case 'ToolbarAll':
			Ext.get('searchType').dom.value = '';
			break;
		case 'ToolbarHire':
			if (pressed)
				Ext.get('searchType').dom.value = '求租';
			else
				Ext.get('searchType').dom.value = '';
			break;
		case 'ToolbarSale':
			if (pressed)
				Ext.get('searchType').dom.value = '求购';
			else
				Ext.get('searchType').dom.value = '';
			break;
	}
	SubmitForm();
};
</script>
<div id="top" class="x-layout-inactive-content">
  <div id="toolbar"></div>
  <div id="search" class="x-form-item x-layer">
	<form id="search_form">
	<input type="hidden" name="searchType" id="searchType" value="" />
	<table border="0" cellpadding="0" cellspacing="0">
	<tr>
	<td width="130">
		<label style="width:30px">用途:</label>
		<select name="purpose" id="purpose">
		<option value="" selected="selected">不限</option>
		<?php foreach($yongtu as $item):?>
		<option value="<?=$item->TreeView_Name;?>"><?=$item->TreeView_Name;?></option>
		<?php endforeach;?>
		</select>
	</td>
	<td width="120">
		<label style="width:30px">客户:</label>
		<input id="req_name" name="req_name" type="text" />
	</td>
	<td width="180">
		<label style="width:30px">售价:</label>
		<input id="price_down" name="price_down" type="text" /> 至
		<input id="price_up" name="price_up" type="text" /> 万元
	</td>
	<td width="150">
		<label style="width:30px">户型:</label>
		<input id="house_typedown" name="house_typedown" type="text" maxlength="2" /> 至
		<input id="house_typeup" name="house_typeup" type="text" maxlength="2" /> 房
	</td>
	<td>
		<div id="grow"></div>
	</td>
	<td width="50">
		<div id="clear"></div>
	</td>
	<td>
		<div id="find"></div>
	</td>
	</td>
	</tr>
	</table>
	</form>
  </div>
</div>
<div id="content" class="y-layout-active-content">
</div>
<div id="bottom" class="x-layout-inactive-content">
</div>
<div id="AddDialog"></div>
<div id="EditDialog"></div>
<div id="SaveLayer"></div>
</body>
</html>